package cn.xspace.wnx.view.chat.chatrow;

import android.content.Context;
import android.text.TextUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.util.LogUtil;

import java.io.File;

import cn.xspace.wnx.R;
import cn.xspace.library.util.picture.BitmapTransformation;
import cn.xspace.chatui.bean.MsgRecordEntity;

/**
 * Created by liuchen on 2016/11/4.
 */

public class ChatRowLocation extends EaseChatRowFile {
    private TextView txtAddress;
    private ImageView imgSnap;

    public ChatRowLocation(Context context, MsgRecordEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(!isMySend() ?
                R.layout.chat_row_received_location : R.layout.chat_row_sent_location, this);
    }

    @Override
    protected void onFindViewById() {
        txtAddress = (TextView) findViewById(R.id.txt_address);
        imgSnap = (ImageView) findViewById(R.id.img_snap);
    }

    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }

    @Override
    protected void onSetUpView() {
        if (message.mFileEntity != null) {
            String localPath = message.mFileEntity.file_url_local;
            txtAddress.setText(message.mFileEntity.address_desc);
            if (isMySend()) {
                if (!new File(localPath).exists()) {
                    //本地图片没了
//                getFileByHttp();

                    if (!TextUtils.isEmpty(message.mFileEntity.file_url_remote)) {
                        Picasso.with(context)
                                .load(message.mFileEntity.file_url_remote)
//                        .resize(200, 200)
                                .placeholder(R.drawable.chat_default_image)
//                        .centerCrop()
                                .error(R.drawable.chat_default_image_error)
                                .transform(new BitmapTransformation(message.mFileEntity.file_url_remote))
                                .into(imgSnap);
                    } else {
                        imgSnap.setImageResource(R.drawable.chat_default_image_error);
                    }

                } else {
                    LogUtil.d(TAG, "加载本地图片");
                    //直接加载本地
                    Picasso.with(context)
                            .load(new File(localPath))
                            .placeholder(R.drawable.chat_default_image)
//                        .centerCrop()
                            .error(R.drawable.chat_default_image_error)
                            .transform(new BitmapTransformation(localPath))
                            .into(imgSnap);
//                showImageView(localPath, imageView, message.mFileEntity.file_url_remote);
                }
                handleSendMessage();
            } else {
                if (!new File(localPath).exists()) {
                    LogUtil.d(TAG, "图片还没有下载" + localPath);
                    //本地图片没了
//                getFileByHttp();

                    if (!TextUtils.isEmpty(message.mFileEntity.file_url_remote)) {
                        Picasso.with(context)
                                .load(message.mFileEntity.file_url_remote)

                                .placeholder(R.drawable.chat_default_image)
//                        .centerCrop()
                                .error(R.drawable.chat_default_image_error)
                                .transform(new BitmapTransformation(message.mFileEntity.file_url_remote))
                                .into(imgSnap);
                    } else {
                        imgSnap.setImageResource(R.drawable.chat_default_image_error);
                    }
                } else {
                    LogUtil.d(TAG, "本地存在图片" + localPath);
                    Picasso.with(context)
                            .load(new File(localPath))
                            .placeholder(R.drawable.chat_default_image)
//                        .centerCrop()
                            .error(R.drawable.chat_default_image_error)
                            .transform(new BitmapTransformation(message.mFileEntity.file_url_remote))
                            .into(imgSnap);
                }
            }
        }
    }

    @Override
    protected void buildSendMap() {
        super.buildSendMap();
        sendMap.put("msg_lat", message.mFileEntity.lat);
        sendMap.put("msg_lng", message.mFileEntity.lng);
        sendMap.put("msg_postion", message.mFileEntity.address_desc);
    }

    @Override
    protected void onBubbleClick() {

    }
}
